﻿package little.bunny;

import java.util.ArrayList;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnKeyListener;
import android.content.Intent;
import android.content.res.Configuration;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.HorizontalScrollView;
import android.widget.ListView;
import android.widget.RadioGroup;
//import android.widget.TextView;
import little.bunny.bean.SortS;
import little.bunny.create_dh.CreateDaoHang;

public class NeiActivity extends Activity{
	private ListView myListView;
	private HorizontalScrollView myHorizontalScrollView;
	//private RadioButton bt1,bt2,bt3;
	private RadioGroup myGroup;
	private CreateDaoHang cdh;
	private int bj ,s ,d ;
	private Button backButton;
	private String url,moban,start,end;
	
	private Handler myHandler= new Handler(){
		@Override
		public void handleMessage(Message msg) {
			// TODO Auto-generated method stub
			super.handleMessage(msg);
		}
		
	};
	private ArrayList<SortS> myListSorts;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.nei_layout);
		
		myHorizontalScrollView=(HorizontalScrollView)findViewById(R.id.horizontalScrollView1);
		myHorizontalScrollView.setHorizontalScrollBarEnabled(false);
		backButton=(Button)findViewById(R.id.back_button);
		backButton.setOnClickListener(new OnClickListener() {
			
			public void onClick(View v) {
				// TODO Auto-generated method stub
				finish();
			}
		});
		
		Intent myIntent=getIntent();
		url=myIntent.getStringExtra("url");
		moban=myIntent.getStringExtra("moban");
		start=myIntent.getStringExtra("start");
		s=myIntent.getIntExtra("startid", 1);
		end=myIntent.getStringExtra("end");
		d=myIntent.getIntExtra("endid", 1);
		bj=myIntent.getIntExtra("bj", 0);
		myListView=(ListView)findViewById(R.id.title_listView);
		myListView.setVerticalScrollBarEnabled(false);
		myGroup=(RadioGroup)findViewById(R.id.linearLayout3);
		
		// 创建进度条
		final ProgressDialog progressDialog = ProgressDialog.show(this, "请稍等",
				"数据加载中...", false);
		progressDialog.setOnKeyListener(new OnKeyListener() {
			
			public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
				// TODO Auto-generated method stub
				if (keyCode == KeyEvent.KEYCODE_BACK){
					progressDialog.dismiss();
				}
				return false;
			}
		});
		// 创建线程
		new Thread() {
			public void run() {
				Looper.prepare();
				try {
					myListSorts=CreateDaoHang.getSortList(url,moban,start, s, end,d);
					if(myListSorts==null||myListSorts.size()==0){
						progressDialog.dismiss();
						new AlertDialog.Builder(NeiActivity.this)
						.setTitle("网络信号异常")
						.setMessage("请查看网络连接，请稍后再试...")
						.setPositiveButton("确定",
								new DialogInterface.OnClickListener() {
									public void onClick(DialogInterface dialog,
											int which) {
										NeiActivity.this.finish();
									}
								}).show();
					}else{
						myHandler.post(addValues);
					}
				} catch (Exception e) {
					// TODO: handle exception
					System.out.println("NeiAcitvity获取服务器传递数据时候，报错。。。。。。。。。。");
					progressDialog.dismiss();
					new AlertDialog.Builder(NeiActivity.this)
					.setTitle("网络信号异常")
					.setMessage("请查看网络连接，请稍后再试...")
					.setPositiveButton("确定",
							new DialogInterface.OnClickListener() {
								public void onClick(DialogInterface dialog,
										int which) {
									
									System.exit(0);
								}
							}).show();
					//System.out.println("我的本机:"+e.getMessage());
					//System.out.println("获取服务器传递数据时候，报错。。。。。。。。。。");
				}
				progressDialog.dismiss();// 隐藏进度条
				Looper.loop();
			}
		}.start();
	
		
		
		
		
	}
	
	final Runnable addValues = new Runnable() {

		public void run() {
			// TODO Auto-generated method stub
			cdh=new CreateDaoHang(NeiActivity.this, myListSorts, myGroup,myListView);
			cdh.createDH(bj);
		}
		
	};
	
	@Override 
    
	public void onConfigurationChanged(Configuration newConfig) { 
		                super.onConfigurationChanged(newConfig); 
		                if (this.getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) { 
			                        // land do nothing is ok 
			                } else if (this.getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) { 
				                        // port do nothing is ok 
				                } 
		        }

	
	
}
